<template>
  <div>
    <div>
      <el-button class="filter-item"
                 style="margin-right: 40px;" v-waves
                 type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="驾驶员姓名" prop="name"
                       sortable="custom" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="驾驶员所属公司" prop="company"
                       sortable="custom" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.company }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" prop="tel"
                       sortable="custom" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.tel }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDel(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination small v-show="total>0"
                :total="total"
                :page.sync="listQuery.page"
                :limit.sync="listQuery.limit"
                @pagination="getList" />


    <el-dialog :title="textMap[dialogStatus]"
               :visible.sync="dialogFormVisible">
      <el-form ref="dataForm"
               :rules="rules"
               :model="temp"
               label-position="left" label-width="130px" style="width: 400px; margin-left:50px;">
        <el-form-item label="驾驶员姓名" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="驾驶员电话" prop="tel">
          <el-input v-model="temp.tel" />
        </el-form-item>
        <el-form-item label="驾驶员所属公司" prop="company">
          <el-input v-model="temp.company" />
        </el-form-item>
        <el-form-item label="登录密码" prop="pwd">
          <el-input v-model="temp.pwd" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary"
                   @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
  import $ from 'jquery';
  import ajax from '@/utils/baseurl'
  import Pagination from '@/components/Pagination'
  import waves from '@/directive/waves'

  var p;
  export default {
    name: 'Driver',
    components: { Pagination },
    directives: { waves },
    data() {
      return {
        temp: {
          name :'',
          tel:'',
          company:'',
          pwd:''
        },
        rules: {
          name: [{ required: true, message: '姓名不能为空', trigger: 'change' }],
          tel: [{ required: true, message: '电话不能为空', trigger: 'change' },
            { pattern: /^1[34578]\d{9}$/,trigger: 'blur', message: '请重新输入！' }],
          company: [{ required: true, message: '公司不能为空', trigger: 'change' }],
          pwd: [{ required: true, message: '密码不能为空', trigger: 'change' }]
        },
        dialogFormVisible:false,
        dialogStatus: '',
        textMap: {
          update: '修改',
          create: '新增'
        },
        listQuery: {
          page: 1,
          limit: 10,
          sort: '+id'
        },
        total: 0,
        tableKey:0,
        listLoading:true,
        list:null
      }
    },
    created() {
      p=this;
      this.getList()
    },
    methods:{
      updateData(){
        this.createData();
        this.getList();
      },
      resetTemp() {
        this.temp = {
          name :'',
          tel:'',
          company:'',
          pwd:''
        }
      },
      createData(){
        //this.resetTemp();
        var p=this;
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            ajax("driver/add",this.temp,function(data){
              if(data.id){
                p.dialogFormVisible = false;
                p.getList();
                p.$notify({
                  // title: '添加',
                  message: p.dialogStatus=='create'?'添加成功':'修改成功',
                  type: 'success',
                  duration: 2000
                });
              }
            })
          }
        })
      },
      handleCreate() {
        this.dialogStatus = 'create';
        this.dialogFormVisible = true;
      },
      handleUpdate(row){
        this.temp = Object.assign({}, row);
        this.dialogStatus = 'update';
        this.dialogFormVisible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()//清除验证
        })

      },
      handleDel(row){
        var p = this;
        this.$confirm('确认删除吗? 是否继续', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          ajax("driver/del",{"id": row.id },function(data){
            if(data){
              p.getList();
              p.$message({
                type: 'success',
                message: '删除成功!'
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      getList(){
        this.listLoading = true;
        ajax("driver/all2",this.listQuery,function(data){
          console.log(data);
          p.list=data.content;
          p.total=data.totalElements;
          p.listLoading=false;
        })

      }
    }
  }
</script>













